package com.hxkcloud.nurse.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.utils.ImageOptionUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;

/**
 * Created by kaka_xiao on 2016/5/23.
 * instructions:
 */
public class PhotoPagerAdapter extends PagerAdapter {
    private ArrayList<String> Urls;
    private DisplayImageOptions options;
    private ImageLoader imageLoader;
    private Context context;
    private ArrayList<View> images;
    private int currentPage;

    public PhotoPagerAdapter(Context context, ArrayList<String> urls) {
        this.context = context;
        Urls = urls;
        imageLoader = ImageLoader.getInstance();
        options = ImageOptionUtils.getInstance().getSimplePicOptions();
        inits();
    }

    private void inits() {
        images = new ArrayList<>();
        for (String url : Urls) {
            View picView = View.inflate(context, R.layout.view_photo, null);
            images.add(picView);
        }
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(images.get(position));
    }

    @Override
    public View instantiateItem(ViewGroup container, final int position) {
        container.addView(images.get(position));
        final PhotoView photoView = (PhotoView) images.get(position).findViewById(R.id.iv_photo);
        String path = Urls.get(position);
        if (!path.startsWith("http"))
            path = ImageDownloader.Scheme.FILE.wrap(path);
        imageLoader.loadImage(path, options, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingStarted(String imageUri, View view) {
                super.onLoadingStarted(imageUri, view);
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                super.onLoadingComplete(imageUri, view, loadedImage);
                photoView.setImageBitmap(loadedImage);
            }
        });
        return images.get(position);
    }

    @Override
    public int getCount() {
        return images.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


}
